package o;

import com.huawei.hms.framework.network.restclient.hwhttp.HttpContants;
import java.io.Closeable;
import java.io.File;
import java.io.Flushable;
import java.io.IOException;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;
import o.fvh.d;

public final class ftw implements Closeable, Flushable {
    private int ʽ;
    private int ˊ;
    int ˋ;
    final fvj ˎ;
    final fvh ˏ;
    int ॱ;
    private int ॱॱ;

    static final class a {
        private static final String ˎ = (fwy.ˋ().ᐝ() + "-Sent-Millis");
        private static final String ॱ = (fwy.ˋ().ᐝ() + "-Received-Millis");
        private final String ʻ;
        private final fut ʼ;
        private final fui ʽ;
        private final String ˊ;
        private final long ˊॱ;
        private final fui ˋ;
        private final String ˏ;
        private final long ͺ;
        private final int ॱॱ;
        @Nullable
        private final fuk ᐝ;

        a(fxw o_fxw) throws IOException {
            long j = 0;
            int i = 0;
            try {
                int i2;
                long parseLong;
                fxg ˎ = fxr.ˎ(o_fxw);
                this.ˊ = ˎ.ॱˋ();
                this.ˏ = ˎ.ॱˋ();
                o.fui.a aVar = new o.fui.a();
                int ˋ = ftw.ˋ(ˎ);
                for (i2 = 0; i2 < ˋ; i2++) {
                    aVar.ˏ(ˎ.ॱˋ());
                }
                this.ˋ = aVar.ˊ();
                fvy ॱ = fvy.ॱ(ˎ.ॱˋ());
                this.ʼ = ॱ.ˋ;
                this.ॱॱ = ॱ.ˎ;
                this.ʻ = ॱ.ˊ;
                aVar = new o.fui.a();
                i2 = ftw.ˋ(ˎ);
                while (i < i2) {
                    aVar.ˏ(ˎ.ॱˋ());
                    i++;
                }
                String ˋ2 = aVar.ˋ(ˎ);
                String ˋ3 = aVar.ˋ(ॱ);
                aVar.ˎ(ˎ);
                aVar.ˎ(ॱ);
                if (ˋ2 != null) {
                    parseLong = Long.parseLong(ˋ2);
                } else {
                    parseLong = 0;
                }
                this.ˊॱ = parseLong;
                if (ˋ3 != null) {
                    j = Long.parseLong(ˋ3);
                }
                this.ͺ = j;
                this.ʽ = aVar.ˊ();
                if (ॱ()) {
                    String ॱˋ = ˎ.ॱˋ();
                    if (ॱˋ.length() > 0) {
                        throw new IOException("expected \"\" but was \"" + ॱˋ + "\"");
                    }
                    fuz o_fuz;
                    fub ˋ4 = fub.ˋ(ˎ.ॱˋ());
                    List ˏ = ˏ(ˎ);
                    List ˏ2 = ˏ(ˎ);
                    if (ˎ.ᐝ()) {
                        o_fuz = fuz.SSL_3_0;
                    } else {
                        o_fuz = fuz.ˋ(ˎ.ॱˋ());
                    }
                    this.ᐝ = fuk.ˊ(o_fuz, ˋ4, ˏ, ˏ2);
                } else {
                    this.ᐝ = null;
                }
                o_fxw.close();
            } catch (Throwable th) {
                o_fxw.close();
            }
        }

        a(fvb o_fvb) {
            this.ˊ = o_fvb.ॱ().ˏ().toString();
            this.ˋ = fvr.ˎ(o_fvb);
            this.ˏ = o_fvb.ॱ().ˋ();
            this.ʼ = o_fvb.ˎ();
            this.ॱॱ = o_fvb.ˊ();
            this.ʻ = o_fvb.ˋ();
            this.ʽ = o_fvb.ʼ();
            this.ᐝ = o_fvb.ʻ();
            this.ˊॱ = o_fvb.ॱˊ();
            this.ͺ = o_fvb.ˏॱ();
        }

        public void ˏ(d dVar) throws IOException {
            int i;
            int i2 = 0;
            fxj ˋ = fxr.ˋ(dVar.ˏ(0));
            ˋ.ˋ(this.ˊ).ॱॱ(10);
            ˋ.ˋ(this.ˏ).ॱॱ(10);
            ˋ.ˋॱ((long) this.ˋ.ˋ()).ॱॱ(10);
            int ˋ2 = this.ˋ.ˋ();
            for (i = 0; i < ˋ2; i++) {
                ˋ.ˋ(this.ˋ.ॱ(i)).ˋ(": ").ˋ(this.ˋ.ˋ(i)).ॱॱ(10);
            }
            ˋ.ˋ(new fvy(this.ʼ, this.ॱॱ, this.ʻ).toString()).ॱॱ(10);
            ˋ.ˋॱ((long) (this.ʽ.ˋ() + 2)).ॱॱ(10);
            i = this.ʽ.ˋ();
            while (i2 < i) {
                ˋ.ˋ(this.ʽ.ॱ(i2)).ˋ(": ").ˋ(this.ʽ.ˋ(i2)).ॱॱ(10);
                i2++;
            }
            ˋ.ˋ(ˎ).ˋ(": ").ˋॱ(this.ˊॱ).ॱॱ(10);
            ˋ.ˋ(ॱ).ˋ(": ").ˋॱ(this.ͺ).ॱॱ(10);
            if (ॱ()) {
                ˋ.ॱॱ(10);
                ˋ.ˋ(this.ᐝ.ॱ().ˋ()).ॱॱ(10);
                ˎ(ˋ, this.ᐝ.ˎ());
                ˎ(ˋ, this.ᐝ.ˊ());
                ˋ.ˋ(this.ᐝ.ˋ().ˏ()).ॱॱ(10);
            }
            ˋ.close();
        }

        private boolean ॱ() {
            return this.ˊ.startsWith("https://");
        }

        private List<Certificate> ˏ(fxg o_fxg) throws IOException {
            int ˋ = ftw.ˋ(o_fxg);
            if (ˋ == -1) {
                return Collections.emptyList();
            }
            try {
                CertificateFactory instance = CertificateFactory.getInstance("X.509");
                List<Certificate> arrayList = new ArrayList(ˋ);
                for (int i = 0; i < ˋ; i++) {
                    String ॱˋ = o_fxg.ॱˋ();
                    fxi o_fxi = new fxi();
                    o_fxi.ˎ(fxh.ॱ(ॱˋ));
                    arrayList.add(instance.generateCertificate(o_fxi.ʽ()));
                }
                return arrayList;
            } catch (CertificateException e) {
                throw new IOException(e.getMessage());
            }
        }

        private void ˎ(fxj o_fxj, List<Certificate> list) throws IOException {
            try {
                o_fxj.ˋॱ((long) list.size()).ॱॱ(10);
                int size = list.size();
                for (int i = 0; i < size; i++) {
                    o_fxj.ˋ(fxh.ˎ(((Certificate) list.get(i)).getEncoded()).ˏ()).ॱॱ(10);
                }
            } catch (CertificateEncodingException e) {
                throw new IOException(e.getMessage());
            }
        }

        public boolean ˊ(fus o_fus, fvb o_fvb) {
            return this.ˊ.equals(o_fus.ˏ().toString()) && this.ˏ.equals(o_fus.ˋ()) && fvr.ˏ(o_fvb, this.ˋ, o_fus);
        }

        public fvb ॱ(o.fvh.a aVar) {
            String ॱ = this.ʽ.ॱ(HttpContants.KEY_CONTENT_TYPE);
            String ॱ2 = this.ʽ.ॱ("Content-Length");
            return new fvb.d().ˎ(new o.fus.a().ˎ(this.ˊ).ˎ(this.ˏ, null).ˏ(this.ˋ).ॱ()).ॱ(this.ʼ).ॱ(this.ॱॱ).ˊ(this.ʻ).ॱ(this.ʽ).ॱ(new b(aVar, ॱ, ॱ2)).ॱ(this.ᐝ).ॱ(this.ˊॱ).ˏ(this.ͺ).ˏ();
        }
    }

    static class b extends fuy {
        @Nullable
        private final String ˊ;
        final o.fvh.a ˎ;
        @Nullable
        private final String ˏ;
        private final fxg ॱ;

        b(final o.fvh.a aVar, String str, String str2) {
            this.ˎ = aVar;
            this.ˊ = str;
            this.ˏ = str2;
            this.ॱ = fxr.ˎ(new fxm(this, aVar.ˋ(1)) {
                final /* synthetic */ b ॱ;

                public void close() throws IOException {
                    aVar.close();
                    super.close();
                }
            });
        }

        public fup ˊ() {
            return this.ˊ != null ? fup.ॱ(this.ˊ) : null;
        }

        public long ˏ() {
            long j = -1;
            try {
                if (this.ˏ != null) {
                    j = Long.parseLong(this.ˏ);
                }
            } catch (NumberFormatException e) {
            }
            return j;
        }

        public fxg ˋ() {
            return this.ॱ;
        }
    }

    final class c implements fvd {
        boolean ˊ;
        private fxx ˋ;
        private final d ˎ;
        private fxx ˏ;
        final /* synthetic */ ftw ॱ;

        c(final ftw o_ftw, final d dVar) {
            this.ॱ = o_ftw;
            this.ˎ = dVar;
            this.ˏ = dVar.ˏ(1);
            this.ˋ = new fxn(this, this.ˏ) {
                final /* synthetic */ c ˋ;

                public void close() throws IOException {
                    synchronized (this.ˋ.ॱ) {
                        if (this.ˋ.ˊ) {
                            return;
                        }
                        this.ˋ.ˊ = true;
                        ftw o_ftw = this.ˋ.ॱ;
                        o_ftw.ॱ++;
                        super.close();
                        dVar.ॱ();
                    }
                }
            };
        }

        public void ˏ() {
            synchronized (this.ॱ) {
                if (this.ˊ) {
                    return;
                }
                this.ˊ = true;
                ftw o_ftw = this.ॱ;
                o_ftw.ˋ++;
                fvg.ॱ(this.ˏ);
                try {
                    this.ˎ.ˊ();
                } catch (IOException e) {
                }
            }
        }

        public fxx ॱ() {
            return this.ˋ;
        }
    }

    public ftw(File file, long j) {
        this(file, j, fwm.ˋ);
    }

    ftw(File file, long j, fwm o_fwm) {
        this.ˎ = new fvj(this) {
            final /* synthetic */ ftw ˏ;

            {
                this.ˏ = r1;
            }

            public fvb ˊ(fus o_fus) throws IOException {
                return this.ˏ.ˎ(o_fus);
            }

            public fvd ˏ(fvb o_fvb) throws IOException {
                return this.ˏ.ˎ(o_fvb);
            }

            public void ˏ(fus o_fus) throws IOException {
                this.ˏ.ˏ(o_fus);
            }

            public void ˎ(fvb o_fvb, fvb o_fvb2) {
                this.ˏ.ॱ(o_fvb, o_fvb2);
            }

            public void ˊ() {
                this.ˏ.ˏ();
            }

            public void ˋ(fvf o_fvf) {
                this.ˏ.ˋ(o_fvf);
            }
        };
        this.ˏ = fvh.ˏ(o_fwm, file, 201105, 2, j);
    }

    public static String ॱ(fur o_fur) {
        return fxh.ˏ(o_fur.toString()).ˋ().ᐝ();
    }

    @Nullable
    fvb ˎ(fus o_fus) {
        try {
            Closeable ˏ = this.ˏ.ˏ(ॱ(o_fus.ˏ()));
            if (ˏ == null) {
                return null;
            }
            try {
                a aVar = new a(ˏ.ˋ(0));
                fvb ॱ = aVar.ॱ(ˏ);
                if (aVar.ˊ(o_fus, ॱ)) {
                    return ॱ;
                }
                fvg.ॱ(ॱ.ᐝ());
                return null;
            } catch (IOException e) {
                fvg.ॱ(ˏ);
                return null;
            }
        } catch (IOException e2) {
            return null;
        }
    }

    @Nullable
    fvd ˎ(fvb o_fvb) {
        d dVar;
        String ˋ = o_fvb.ॱ().ˋ();
        if (fvt.ˎ(o_fvb.ॱ().ˋ())) {
            try {
                ˏ(o_fvb.ॱ());
                return null;
            } catch (IOException e) {
                return null;
            }
        } else if (!ˋ.equals(HttpContants.HTTP_METHOD_GET) || fvr.ॱ(o_fvb)) {
            return null;
        } else {
            a aVar = new a(o_fvb);
            try {
                d ˋ2 = this.ˏ.ˋ(ॱ(o_fvb.ॱ().ˏ()));
                if (ˋ2 == null) {
                    return null;
                }
                try {
                    aVar.ˏ(ˋ2);
                    return new c(this, ˋ2);
                } catch (IOException e2) {
                    dVar = ˋ2;
                    ॱ(dVar);
                    return null;
                }
            } catch (IOException e3) {
                dVar = null;
                ॱ(dVar);
                return null;
            }
        }
    }

    void ˏ(fus o_fus) throws IOException {
        this.ˏ.ॱ(ॱ(o_fus.ˏ()));
    }

    void ॱ(fvb o_fvb, fvb o_fvb2) {
        a aVar = new a(o_fvb2);
        try {
            d ˋ = ((b) o_fvb.ᐝ()).ˎ.ˋ();
            if (ˋ != null) {
                aVar.ˏ(ˋ);
                ˋ.ॱ();
            }
        } catch (IOException e) {
            ॱ(null);
        }
    }

    private void ॱ(@Nullable d dVar) {
        if (dVar != null) {
            try {
                dVar.ˊ();
            } catch (IOException e) {
            }
        }
    }

    public void flush() throws IOException {
        this.ˏ.flush();
    }

    public void close() throws IOException {
        this.ˏ.close();
    }

    synchronized void ˋ(fvf o_fvf) {
        this.ʽ++;
        if (o_fvf.ˋ != null) {
            this.ˊ++;
        } else if (o_fvf.ˊ != null) {
            this.ॱॱ++;
        }
    }

    synchronized void ˏ() {
        this.ॱॱ++;
    }

    static int ˋ(fxg o_fxg) throws IOException {
        try {
            long ͺ = o_fxg.ͺ();
            String ॱˋ = o_fxg.ॱˋ();
            if (ͺ >= 0 && ͺ <= 2147483647L && ॱˋ.isEmpty()) {
                return (int) ͺ;
            }
            throw new IOException("expected an int but was \"" + ͺ + ॱˋ + "\"");
        } catch (NumberFormatException e) {
            throw new IOException(e.getMessage());
        }
    }
}
